<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="98px" ref="queryForm" size="small"
             v-show="showSearch">

      <el-form-item label="框架编号" prop="projectCode">
        <el-input @keyup.enter.native="handleQuery" clearable
                  placeholder="请输入框架编号" v-model="queryParams.projectCode"/>
      </el-form-item>

      <el-form-item label="订单编号" prop="code">
        <el-input @keyup.enter.native="handleQuery" clearable
                  placeholder="请输入订单编号" v-model="queryParams.code"/>
      </el-form-item>
      <el-form-item label="业务员" prop="master">
        <el-input @keyup.enter.native="handleQuery" clearable
                  placeholder="请输入业务员" v-model="queryParams.salesmanName"/>
      </el-form-item>
      <el-form-item label="客户名称" prop="customName">
        <el-input @keyup.enter.native="handleQuery" clearable
                  placeholder="请输入客户名称" v-model="queryParams.customName"/>
      </el-form-item>

      <!--<el-form-item label="订单状态" prop="status">
        <el-select clearable placeholder="选择订单状态" style="width: 200px" v-model="queryParams.status">
          <el-option :key="dict.value"
                     :label="dict.label" :value="dict.value" v-for="dict in dict.type.pro_status"/>
        </el-select>
      </el-form-item>-->

      <el-form-item label="下单日期" prop="orderDate">
        <el-date-picker clearable
                        v-model="queryParams.orderDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下单日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="需求日期" prop="deliveryTime">
        <el-date-picker clearable
                        v-model="queryParams.deliveryTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下单日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
        <el-button type="warning" plain icon="el-icon-shopping-cart-full" :disabled="multiple" size="mini"
                   @click="handleBatchPleaseBuy({})" >批量请购
        </el-button>

        <el-button type="warning" plain icon="el-icon-shopping-cart-full" :disabled="multiple" size="mini"
                   @click="handleGoodsMerge()" >批量请购（合并）
        </el-button>



      </el-form-item>
    </el-form>
    <el-table :data="dataList" :max-height="tableMaxHeight" @selection-change="handleSelectionChange"
              v-loading="loading" @sort-change="handleSortChange">
      <el-table-column align="center" type="selection" width="55"/>

      <el-table-column align="center" label="框架编号" prop="projectCode" show-overflow-tooltip/>
      <el-table-column sortable align="center" label="订单编号" prop="code" width="240">
        <template slot-scope="scope">
          <el-tooltip class="item" content="点击查看详情" effect="dark" placement="left-start">
            <span @click="handleUpdateDetail(scope.row)"
                  style="color: #00afff;cursor:pointer;">{{ scope.row.code }}</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column sortable align="center" label="买方名称" prop="customName" show-overflow-tooltip/>
      <el-table-column sortable align="center" label="业务员" prop="salesmanName"/>
      <el-table-column sortable align="center" label="需求日期" prop="deliveryTime" />
      <el-table-column sortable align="center" label="订单状态" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_status" :value="scope.row.status"/>
        </template>
      </el-table-column>

      <el-table-column sortable label="订单类型" align="center" prop="goodsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_goods_type" :value="scope.row.goodsType"/>
        </template>
      </el-table-column>

      <el-table-column sortable align="center" label="地区" prop="area">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_area" :value="scope.row.area"/>
        </template>
      </el-table-column>

      <el-table-column sortable label="下单日期" align="center" prop="orderDate" width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注" prop="information"/>
      <el-table-column align="center" label="附件" prop="attachment">
        <template slot-scope="scope">
          <el-tooltip class="item" content="点击下载附件" effect="dark" placement="left-start">
            <a :href="baseUrl + scope.row.attachment" style="color: #15B6FF;" target="_blank" download v-if="scope.row.attachment">附件</a>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="300">
        <template slot-scope="scope">
          <el-button @click="handleChoseGoods(scope.row)" v-if="scope.row.status==1" icon="el-icon-plus" size="mini"
                     type="text">
            产品请购
          </el-button>
          <el-button @click="handleOrderDetail(scope.row)" icon="el-icon-view" size="mini" type="text">
            产品明细
          </el-button>
          <el-popconfirm :title="`确认要弃审该订单吗？`" @confirm="rejectOrder(scope.row)"
                         style="margin-left: 10px;"  v-if="scope.row.status==1">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-close">
              弃审
            </el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <pagination :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList" v-show="total>0"/>

    <pleaseBuy-form-goods-new @ok="getList" ref="formGoods"></pleaseBuy-form-goods-new>
    <please-buy-form-goods-by-ids @ok="getList" ref="formGoodsByIds"></please-buy-form-goods-by-ids>
    <goods-order-merge-list ref="goodsOrderMergeList"></goods-order-merge-list>
    <pleaseBuy-look-goods @ok="getList" ref="lookGoodsForm"></pleaseBuy-look-goods>
    <goods-orderin-chose @ok="getList" ref="formChoseOther"></goods-orderin-chose>
    <project-base-info-form-for-prj @ok="getList" ref="ProjectBaseInfoFormForPrj"></project-base-info-form-for-prj>
    <order-goods-list @ok="getList" ref="orderGoodsList"></order-goods-list>


  </div>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import Treeselect from "@riophae/vue-treeselect";
import ProjectBaseInfoFormForPrj from "@/views/develop/projectBaseInfo/module/ProjectBaseInfoFormForPrj";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {treeselect} from "@/api/system/dept";
import PleaseBuyFormGoodsNew from "../pleaseBuy/prjForGoodsIn/PleaseBuyFormGoodsNew";
import PleaseBuyLookGoods from '../pleaseBuy/prjForGoodsIn/PleaseBuyLookGoods';
import GoodsOrderinChose from "@/views/develop/pleaseBuy/prjForGoodsIn/GoodsBuyChose";
import OrderGoodsList from "@/views/develop/goodsSale/module/OrderGoodsList.vue";
import PleaseBuyFormGoodsByIds from "@/views/develop/pleaseBuy/prjForGoodsIn/PleaseBuyFormGoodsByIds.vue";
import GoodsOrderMergeList from "@/views/develop/projectBaseInfo/prjForGoodsIn/GoodsOrderMergeList.vue";

export default {
  name: "ProjectYiQgList",
  dicts: ['yes_no', 'fund_status', 'tax_status', 'order_area','pro_status','order_goods_type'],
  components: {
    GoodsOrderMergeList,
    PleaseBuyFormGoodsByIds,
    ProjectBaseInfoFormForPrj,
    OrderGoodsList,
    Treeselect,
    PleaseBuyFormGoodsNew,
    PleaseBuyLookGoods,
    GoodsOrderinChose
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单基础信息管理表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        yearStr: null,
        pageNum: 1,
        pageSize: 20,
        name: null,
        status: null,
        orderDate: null,
        salesmanName: null,
        customId: null,
        customName: null,
        companyName: null,
        contractDate: null,
        contractMoney: null,
        deliveryTime: null,
        auditMoney: null,
        checkDate: null,
        checkState: null,
        retentionDate: null,
        fundCallback: null,
        orderByColumn: "create_time",
        isAsc: "desc",
        params: {},
      },
      url: {
        list: '/projectBaseInfo/projectBaseInfo/listNotTiejian',
        remove: '/projectBaseInfo/projectBaseInfo/',
        storeReject: '/projectBaseInfo/projectBaseInfo/storeReject/',
        export: '/projectBaseInfo/projectBaseInfo/export',
        submitProjectScalaDo: '/projectBaseInfo/projectBaseInfo/submitProjectScalaDo',
      },
      tableMaxHeight: window.innerHeight - 320,
      // 部门树选项
      deptOptions: [],
      baseUrl: process.env.VUE_APP_BASE_API,
    };
  },
  created() {
    this.queryParams.status = this.$route.query.status;
    this.getList();
    //this.getTreeselect();
  },
  methods: {
    handleChoseOther(row) {
      this.$refs.formChoseOther.chose(row.id, "2");
    },
    submitProjectScalaDo(row) {
      getAction(this.url.submitProjectScalaDo, {id: row.id, status: "4"}).then(response => {
        /*this.refreshPageData();
        this.$modal.msgSuccess("提交成功!");
        this.open = false;
        this.handleQuery();*/
        this.getList();
        this.$modal.msgSuccess("提交成功!");
      }).catch(() => {
      });
    },
    lookGoods(row) {
      this.$refs.lookGoodsForm.handleUpdate(row);
    },
    initQuery() {
      let that = this;
      if (that.$route.query.dataFlag) {
        that.queryParams.params.dataFlag = that.$route.query.dataFlag;
      }
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    exportExcel(id) {
      let that = this;
      that.loading = false;
      that.$confirm('是否确认导出详细信息信息?', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        downloadFile('/projectBaseInfo/projectBaseInfo/exportExcel', id);
      });
    },
    fapiaoHX(row, type) {
      let that = this;
      that.$refs.InvoiceInfoListDialog.showAllInvoices(row);
    },
    handleAddFeeInfo(row) {
      let that = this;
      // that.$refs.ProjectFeeInfoFormForPrj.handleAddNew(row);
      that.$refs.ProjectBaseInfoFormGoodsFee.handleUpdate(row);
    },
    handleAddInvoiceInfo(row) {
      let that = this;
      that.$refs.ProjectInvoiceInfoFormForPrj.handleAddNew(row);
    },

    handleUpdateDetail(row) {
      this.$refs.ProjectBaseInfoFormForPrj.handleUpdateDetail(row);
    },
    /** 查询项目基础信息管理列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.form.handleAdd();
    },
    handleChoseGoods(row) {
      this.$refs.formGoods.handleUpdate(row);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    handleFileSuccess(response, file, fileList) {
      this.$refs.uploadDialog.open = false;
      this.$refs.uploadDialog.isUploading = false;
      if (response.data.code == "200") {
        this.$alert("导入数据：" + response.data.num + "条！更新数据：" + response.data.numupdate + "条！" + response.data.errorText, "导入结果", {dangerouslyUseHTMLString: true});
      } else {
        this.$alert("导入数据：" + response.data.num + "条！更新数据：" + response.data.numupdate + "条！" + response.data.errorText, "导入结果", {dangerouslyUseHTMLString: true});
      }
      this.getList();
    },

    showUploadDialog() {
      this.$refs.uploadDialog.title = "信息导入";
      this.$refs.uploadDialog.tmplName = "订单信息模板.xlsx";
      this.$refs.uploadDialog.fileName = "订单信息.xlsx";
      this.$refs.uploadDialog.uploadUrl = "/projectBaseInfo/projectBaseInfo/importData";
      this.$refs.uploadDialog.show();
    },
    /** 导出按钮操作 */
    handleExport() {
      let that = this;
      that.loading = false;
      that.$confirm('是否确认导出项目信息表?', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        downloadFile('/projectBaseInfo/projectBaseInfo/exportBaseInfo', that.queryParams);
      });
    },


    handleOrderDetail(row) {
      this.$refs.orderGoodsList.handleOrderGoodsShow(row);
    },

    handleBatchPleaseBuy(row){
      const ids = row.id || this.ids;
      let list = this.dataList;
      for (const id of ids) {
        for (let i = 0; i < list.length; i++) {
          if (list[i].id === id) {
            if(list[i].status !== '1'){
              this.$modal.msgError("批量请购，请选择提交状态的订单！");
              return;
            }
          }

        }
      }
      this.$refs.formGoodsByIds.handlePleaseByIds(ids);
    },

    handleGoodsMerge() {
      const ids = this.ids;
      this.$refs.goodsOrderMergeList.show(ids);
    },


    rejectOrder(row){
      const ids = row.id || this.ids;
      getAction(this.url.storeReject+ids, {}).then(response => {
        this.getList();
        this.$modal.msgSuccess("弃审成功");
      }).catch(() => {
      });

    },
    handleSortChange(column) {
      const { prop, order } = column;
      this.queryParams.orderByColumn = prop;
      this.queryParams.isAsc = order === 'ascending' ? 'asc' : 'desc';
      this.handleQuery();
    },


  }
};
</script>
